<?php
namespace app\common\controller;
use think\Request;
use think\Controller;

class Init extends Controller
{
    public function _initialize()
    {
        $this->siteInit();
        $this->adminInit();
        $this->userInit();
        $this->addonsInit();
    }
    
    public function siteInit()
    {
        $this->assign('mod',$this->request->module());
        $this->assign('ctr',$this->request->controller());
        $this->assign('act',$this->request->action());
        if (SITE_HOST) {
            $this->assign('assets','public/assets');
            config('assets','public/assets');
        } else {
            $this->assign('assets','assets');
            config('assets','assets');
        }
        $this->assign('tplPath',config('tplPath'));
        
        $settingInfo = config('settingInfo');
        $this->settingInfo = $settingInfo;
        $this->assign('settingInfo',$settingInfo);
        
        $domain = config('domain');
        $this->domain = $domain;
        $this->assign('domain',$domain);
        
        $domains = config('domains');
        $this->domains = $domains;
        $this->assign('domains',$domains);
        
        $this->domainStatic = config('domainStatic');
        $this->assign('domainStatic',$this->domainStatic);
        
        if ($this->settingInfo['runUrl']) {
//          $this->siteUrl = $this->domainStatic . substr($this->request->url(),0,strlen($domain));
            $this->siteUrl = $this->domainStatic . $this->request->url();
        } else {
            $this->siteUrl = $this->domainStatic . $this->request->url();
        }
        $this->assign('siteUrl',$this->siteUrl);
        $this->currentUrl = $this->siteUrl;
        $this->assign('currentUrl',$this->currentUrl);
        
        $tplName = config('tplName');
        $this->tplName = $tplName;
        $this->assign('tplName',$tplName);
        
        $returnUrl = @htmlspecialchars($_SERVER['HTTP_REFERER']);
        $this->returnUrl = $returnUrl;
        $this->assign('returnUrl', $returnUrl);
        
        $this->assign('itemDetailId','');
        $this->assign('categoryUrlName','');
        
        $this->dataId = config('dataId');
        $this->assign('dataId',$this->dataId);
        $this->siteId = config('siteId');
        $this->assign('siteId',$this->siteId);
        
        $this->assign('isBaidu',isBaidu(Request::instance()->ip()));
        
        $this->keyInfo = [];
        $addons = db('BaseKey')->select();
        foreach ($addons as $k => $v) {
            $this->keyInfo[$v['key']] = $v['val'];
        }
        config('keyInfo',$this->keyInfo);
        $this->assign('keyInfo',$this->keyInfo);
        
        $this->assign('config',config());
    }
    
    public function adminInit()
    {
    	$adminTokenInfo = null;
        $adminToken = cookie('adminToken');
        if ($adminToken) {
            $adminTokenInfo = db('BaseAdminToken')->where('token',$adminToken)->find();
        }
        if ($adminTokenInfo) {
            $this->adminInfo = model('app\\admin\\model\\BaseAdmin')->getItemInfo($adminTokenInfo['uid']);
            $this->adminId = $this->adminInfo['uid'];
            if ($this->adminInfo['status'] == 1) {
                db('BaseAdminToken')->where('token',$adminToken)->delete();
                $this->error('抱歉, 你的账号已经被禁止登录','/');
            }
	    	cookie('adminToken',$adminToken,360000);
            $this->isAdmin = $this->adminInfo['uid'];
        } else {
            $this->adminInfo = null;
            $this->adminId = '';
            $this->isAdmin = '';
        }
        
        config('adminId',$this->adminId);
        config('adminInfo',$this->adminInfo);
        config('isAdmin',$this->isAdmin);
        
        $this->assign('adminId',$this->adminId);
        $this->assign('adminInfo',$this->adminInfo);
        $this->assign('isAdmin',$this->isAdmin);
        
    }
    
    public function userInit()
    {
        $userTokenInfo = null;
        $userToken = cookie('userToken');
        if ($userToken) {
            $userTokenInfo = db('UsersToken')->where('token',$userToken)->find();
        }
        if ($userTokenInfo) {
			try {
	            $this->userInfo = model('app\\user\\model\\Users')->getItemAllInfo($userTokenInfo['uid']);
	            $this->userInfo['groupInfo'] = db('UsersGroup')->where('group_id',$this->userInfo['group_id'])->find();
	            try{$this->userInfo['msgCount'] = db('BaseMsg')->where('status',0)->where('uid',$this->userInfo['uid'])->count();}catch(\Exception $e) {}
	            $this->userId = $this->userInfo['uid'];
	            if ($this->userInfo['status'] == 1) {
	                db('UsersToken')->where('token',$userToken)->delete();
	                $this->error('抱歉, 你的账号已经被禁止登录','/');
	            }
	            cookie('userToken',$userToken,360000);
	            $this->isUserAdmin = ($this->userInfo['group_id'] == 1) ? 1 : 0;
			} catch (\Exception $e) {}
        } else {
            $this->userInfo = null;
            $this->userId = '';
            $this->isUserAdmin = '';
        }
        
        config('userId',$this->userId);
        config('userInfo',$this->userInfo);
        config('isUserAdmin',$this->isUserAdmin);
        
        $this->assign('userId',$this->userId);
        $this->assign('userInfo',$this->userInfo);
        $this->assign('isUserAdmin',$this->isUserAdmin);
        
        if (!$this->settingInfo['systemStatus']) {
            if ($this->request->module() != 'admin') {
                if ($this->userInfo['group_id'] != 1) {
                    if (!$this->request->isPost()) {
                        $this->error('站点关闭中...','');
                    }
                }
            }
        }
    }
    
    
    public function addonsInit()
    {
        $this->addonsInfo = [];
        $this->globalActionList = [];
        $addons = db('BaseAddons')->select();
        foreach ($addons as $k => $v) {
            $this->addonsInfo[$v['name']] = $v['status'];
            if ($v['status']) {
                if ($v['is_global'] == 1) {
                    $this->globalActionList[] = $v;
                }
            }
        }
        config('addonsInfo',$this->addonsInfo);
        $this->assign('addonsInfo',$this->addonsInfo);
        config('globalActionList',$this->globalActionList);
    }
    
    public function siteView($parent, $vars = [] ,$replace = [], $config = [])
    {
        $globalActionList = config('globalActionList');
        if ($globalActionList) {
            foreach ($globalActionList as $key => $val) {
                $addonsName = $val['name'];
                if (config('addonsInfo')[$addonsName]) {
                    $addonsNameSpace = "addons" . "\\" . $addonsName . "\\" . "controller" . "\\" . "GlobalAction";
                    try {
                        model($addonsNameSpace)->$addonsName();
                    } catch (\Exception $e) {
                        continue;
                    }
                }
            }
        }
        
        return $this->fetch('/' .$parent,$vars,$replace, $config);
    }

}
